#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>

int read_ret_buf(FILE *pfp, char *retbuf, int buflen)
{
	int cnt = 0;
	while(fgets(retbuf, buflen, pfp) != NULL)
	{
		if(cnt > 1)
		{
			fprintf(stderr, "read ret buf out of retbuf size:%d", buflen);
			return -1;
		}
		cnt++;
	}
	return 0;
}

int exec_cmd_getret(char * cmdstr, char *retbuf, int buflen)
{
	int ret = 0;
	FILE *pfp = NULL;
	pfp = popen(cmdstr,"r");
	if(!pfp)
	{
		fprintf(stderr, "error exec cmd: %s", cmdstr);
		return -1;
	}
	ret = read_ret_buf(pfp, retbuf, buflen);
	pclose(pfp);

	return ret;
}

int check_wifi(void)
{
	int ret = 0;
	char cmdbuf[1024]={0};
	char retbuf[1024]={0};

	sprintf(cmdbuf, "busybox ifconfig | grep wlan0");	
	ret = exec_cmd_getret(cmdbuf, retbuf, 1024);
	if(ret != 0)
	{		
		ret = -1;
	}

	if(strstr(retbuf,"wlan0") == NULL)
	{
		ret = -2;
	}

	return ret;
}	

int main(int argc, char **argv)
{
	int ret   = -1;
	ret = check_wifi();
	if(0 == ret)
	{
		printf("[OK]\n");
	}else
	{
		printf("[ERROR]\n");
	}	
	return ret;
}
